package com.cb.web.wasf.sample.model;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.persist.Transactional;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 * @author CBO
 */
@Transactional
public class ProfilManager {

    @Inject
    Provider<EntityManager> entityManagerProvider;

    public ProfilBO getByName(String name) {
        return entityManagerProvider.get().find(ProfilBO.class, name);
    }

    public List<ProfilBO> getAll() {
        CriteriaBuilder cb = entityManagerProvider.get().getCriteriaBuilder();
        CriteriaQuery<ProfilBO> query = cb.createQuery(ProfilBO.class);
        Root<ProfilBO> u = query.from(ProfilBO.class);
        query.orderBy(cb.asc(u.get(ProfilBO_.name)));
        return entityManagerProvider.get().createQuery(query).getResultList();
    }

    public ProfilBO create(String name, String description) {
        ProfilBO profil = new ProfilBO();
        profil.setName(name);
        profil.setDescription(description);
        entityManagerProvider.get().persist(profil);
        return profil;
    }
}
